package cn.edu.ctbu.labtest.webapi;

import cn.edu.ctbu.labtest.core.PageUtils;
import cn.edu.ctbu.labtest.domain.Student;
import cn.edu.ctbu.labtest.helper.R;
import cn.edu.ctbu.labtest.helper.RUtil;
import cn.edu.ctbu.labtest.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/webapi/student")
public class StudentRestController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/findbyname")
    public R<Student> findByName(String name, String order, Integer limit, Integer offset){
        Page<Student> all;
        Pageable pageable = PageRequest.of(offset/limit,limit,
                Sort.by("id").descending());

        if(name==null||name.equals("")){
            all=studentService.findAll(pageable);
        }
        else{
            all=studentService.findByNameLike("%"+name+"%",pageable);
        }
        return RUtil.success(all);
    }

    /**
     * 读取的url:/webapi/student/getByPage
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getByPage")
    public PageUtils getByPage(@RequestParam(value ="page",defaultValue = "0")Integer page,
                               @RequestParam(value = "size",defaultValue = "10")Integer size,
                               @RequestParam(value = "name",defaultValue = "")String name)
    {

        //按id排倒序
        Sort sort=Sort.by("id").descending();
        Page<Student> studentPage;
        if(StringUtils.isEmpty(name)){
            Pageable pageable=PageRequest.of(page,size,sort);
            studentPage=studentService.findAll(pageable);
        }
        else{
            Student student=new Student();
            student.setName(name);

            Pageable pageable=PageRequest.of(0,10,sort);
            ExampleMatcher matcher=ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());//模糊查询,即%{name}%
            Example<Student> example=Example.of(student,matcher);
            studentPage=studentService.findAll(example,pageable);
        }
        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return  pageUtils;
    }

    /**
     * 读取的url:/webapi/student/list
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students=studentService.findAll();
        return students;
    }

    /**
     * 读取的url:/webapi/student/findByName
     */
    @GetMapping("/findByName")
    public  List<Student> findByName(String name){
        List<Student> all;
        if(StringUtils.isEmpty(name)){
            all=studentService.findAll();
        }
        else{
            all=studentService.findByName(name);
        }
        return all;
    }


    @GetMapping("/findByNameLike")
    public  Page<Student> findByNameLike(String name,Pageable pageable){
        Page<Student> all;
        if(StringUtils.isEmpty(name)){
            all=studentService.findAll(pageable);
        }
        else{
            all=studentService.findByNameLike(name,pageable);
        }
        return all;
    }

//    @GetMapping("/findByNameLike")
//    public  List<Student> findByNameLike(String name){
//        List<Student> all;
//        if(StringUtils.isEmpty(name)){
//            all=studentService.findAll();
//        }
//        else{
//            all=studentService.findByNameLike(name);
//        }
//        return all;
//    }


    /**
     * 读取的url:/webapi/student/get/id
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
        Student students=studentService.getById(id);
        students.setPassword("");
        return students;
    }

    /**
     * 新增学生，/webapi/student/insert
     */
    @PostMapping("/insert")
    public Student insert(Student student){
        Student student1=studentService.insert(student);
        return student1;
    }

    /**
     * 更新学生，/webapi/student/update
     */
    @PutMapping("/update")
    public Student update(Student student){
        //读取旧的数据
        Student oldStudent=studentService.getById(student.getId());
        if(StringUtils.isEmpty(student.getPassword())){
            //如果没有新密码，则使用旧密码
            student.setPassword(oldStudent.getPassword());
        }
        Student student1=studentService.insert(student);
        return student1;
    }

    /**
     * 删除学生，/webapi/student/delete
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        studentService.delete(id);
    }

//    public void delete(Student student){
//       studentService.delete(student);
//    }



}








